#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <error.h>
#include <strings.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/input.h> // 系统定义输入设备操作的API
#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h>
#include <time.h>

int color[] = { 0x000000, 
                0xFFFFFF, 
                0xFF0000, 
                0x00FF00, 
                0x0000FF, 
                0xFFFF00, 
                0x00FFFF, 
                0xFF00FF
            };

int main(int argc, char *argv[]){
    int lcd = open("/dev/fb0", O_RDWR);
    if(lcd == -1){
        perror("open lcd");
        exit(1);
    }

    char *fbmem = mmap(NULL, 800*480*4, PROT_WRITE, MAP_SHARED, lcd, 0);
    memset(fbmem, 0, 800*480*4);

    int touch = open("/dev/input/event0", O_RDWR);
    if(touch == -1){
        perror("open touch");
        exit(1);
    }
    srand((unsigned)time(NULL));
    while(1){
        struct input_event ev;
        read(touch, &ev, sizeof(ev));
        if(ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value > 0){
            //生成0-7的随机数
            int color_index = rand() % 8;
            int pixel_count = 800 * 480;  // 总像素数
            for (int i = 0; i < pixel_count; i++) {
                memcpy(fbmem + i*4, &color[color_index], 4);  // 每个像素复制4字节
            }
        }
    }
    return 0;
}